package com.xbts.screen.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.xbts.common.core.enums.DateTypeEnum;
import com.xbts.common.core.enums.WorkshopTypeEnum;
import com.xbts.common.core.utils.DateUtils;
import com.xbts.common.core.web.domain.BaseHistogramResult;
import com.xbts.common.core.web.domain.BasePieChartResult;
import com.xbts.screen.domain.dto.HistogramDto;
import com.xbts.screen.domain.vo.WorkshopPassRateRecordVO;
import com.xbts.screen.domain.workshop.dto.CommonQueryDTO;
import com.xbts.screen.domain.workshop.vo.ProductionScheduleVO;
import com.xbts.screen.domain.workshop.vo.RealTimeAchievementRateVO;
import com.xbts.screen.domain.workshop.vo.SamplingPassRateTrendVO;
import com.xbts.screen.domain.workshop.vo.StatisticalRateGroupByTimeVO;
import com.xbts.screen.mapper.MaterialQualityMapper;
import com.xbts.screen.service.IMaterialQualityService;
import com.xbts.screen.service.IWorkshopCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 物料品质 MATERIALQUALITY
 */
@Service
public class MaterialQualityServerImpl implements IMaterialQualityService {

    @Autowired
    private MaterialQualityMapper materialQualityMapper;
    @Autowired
    private IWorkshopCommonService workshopCommonService;

    @DS("wecan")
    @Override
    public BaseHistogramResult samplingPassRateTrend() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.PACKAGING.getErpValue().split(",")));
        return workshopCommonService.passRateTrend(dto);
    }

    @DS("wecan")
    @Override
    public List<BasePieChartResult> samplingBadStatistics() {
        List<BasePieChartResult> list = materialQualityMapper.samplingBadStatistics();
        if (list != null && list.size() > 0) {
            List<BasePieChartResult> results = new ArrayList<>();
            int count = 0;
            int other = 0;
            for (int i = 0; i < list.size(); i++) {
                BasePieChartResult result = list.get(i);
                Integer value = Integer.valueOf(result.getValue());
                count += value;
                if (i < 5) {
                    results.add(result);
                } else {
                    other += value;
                }
            }
            if(list.size() > 5) {
                BasePieChartResult basePieChartResult = new BasePieChartResult();
                basePieChartResult.setType("其他");
                basePieChartResult.setValue(String.valueOf(other));
                results.add(basePieChartResult);
            }

            int finalCount = count;
            results.forEach(item -> {
                item.setRatio(String.format("%.2f", Double.parseDouble(item.getValue()) * 100 / finalCount));
            });
            return results;
        }
        return list;
    }

    @DS("wecan")
    @Override
    public List<WorkshopPassRateRecordVO> passRateRecord() {
        List<String> time = DateUtils.getLast21DaysRange();
        return materialQualityMapper.passRateRecord(time.get(0), time.get(1));
    }

    @DS("wecan")
    @Override
    public SamplingPassRateTrendVO materialTypeSampling() {
        CommonQueryDTO dto = new CommonQueryDTO();
        List<String> last21DaysRange = DateUtils.getLast21DaysRange();
        dto.setStartTime(last21DaysRange.get(0));
        dto.setEndTime(last21DaysRange.get(1));
        SamplingPassRateTrendVO vo = materialQualityMapper.materialTypeSampling(dto);
        vo.setLotRejectNum("0");
        vo.setLotRejectRate("0");
        return vo;
    }
}